﻿using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Threading;

namespace MemoryCacheDemo01
{
    class Program
    {
        static void Main(string[] args)
        {

            var options = Options.Create(new MemoryCacheOptions());
            IMemoryCache cache = new MemoryCache(options);

            cache.Set("name", "祥子");

            //cache.Remove("name");//移除
            Console.WriteLine("name的值是:" + cache.Get("name"));

            cache.GetOrCreate("mykey", c =>
            {
                return "我是值";
            });
            Console.WriteLine("mykey的值是:" + cache.Get("mykey"));

            //设置过期时间
            cache.Set("testKey", "111", TimeSpan.FromSeconds(2));
            Console.WriteLine("testKey的值是:" + cache.Get("testKey"));
            Thread.Sleep(2000);
            Console.WriteLine("testKey的值是:" + cache.Get("testKey"));

            string aa = "";
            cache.Set("doKey","I love you");
            if (cache.TryGetValue("doKey", out aa))
            {
                Console.WriteLine("存在doKey,值为" + aa);
            }


            Console.WriteLine("Hello World!");
        }
    }
}
